package cn.com.jrr.core.common.mytag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import cn.com.jrr.core.common.StringUtils;
import cn.com.jrr.core.model.dictionary.CodeType;
import cn.com.jrr.core.service.dictionary.CodeTypeService;
public class TagSelects extends RequestContextAwareTag {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private CodeTypeService codeTypeService;
    //字段名称
    private String name;
    //数据字典编码
    private String code;
    //当前选中
    private String selected;
    //是否添加--请选择--
    private boolean defaltValue=true;
    //id属性
    private String id;
    //class属性
    private String cssClass;
    //onchange 事件
    private String onchange;
    //onclick 事件
    private String onclick;


    @Override
    protected int doStartTagInternal() throws JspTagException  {
        setService();
        List<CodeType> codeTypeList=null;
        if(StringUtils.isBlank(code)){
            codeTypeList = codeTypeService.listByCode(code);
        }
        //JspWriter out = getJspContext().getOut();
        JspWriter out = pageContext.getOut();
        StringBuffer sb = new StringBuffer() ;
        if(StringUtils.isBlank(name)){
            sb.append("<select name='"+name+"'");
        }
        if(StringUtils.isBlank(id)){
            sb.append(" id='"+id+"'");
        }
        if(StringUtils.isBlank(cssClass)){
            sb.append(" class='"+cssClass+"'");
        }
        if(StringUtils.isBlank(onchange)){
            sb.append(" onchange='"+onchange+"'");
        }
        if(StringUtils.isBlank(onclick)){
            sb.append(" onclick='"+onclick+"'");
        }
        sb.append(">");
        if(defaltValue){
            sb.append("<option value='' selected='selected'>--请选择--</option>");
        }
        for (CodeType ct : codeTypeList) {
            if(selected==ct.getTypeCode()){
                sb.append("<option value='"+ct.getTypeCode()+"' selected='selected'>");
            }else{
                sb.append("<option value='"+ct.getTypeCode()+"'>");
            }
            sb.append(ct.getTypeName()+"</option>");
        }
        try {
            out.print(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE; // 表示处理完标签后继续执行以下的JSP网页
        // return SKIP_PAGE; //表示不处理接下来的JSP网页
    }
    private void setService(){
        this.codeTypeService = (CodeTypeService)this.getRequestContext().getWebApplicationContext().getBean("codeTypeService");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getSelected() {
        return selected;
    }
    public void setSelected(String selected) {
        this.selected = selected;
    }
    public boolean getDefaltValue() {
        return defaltValue;
    }
    public void setDefaltValue(boolean defaltValue) {
        this.defaltValue = defaltValue;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCssClass() {
        return cssClass;
    }
    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }
    public String getOnchange() {
        return onchange;
    }
    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }
    public String getOnclick() {
        return onclick;
    }
    public void setOnclick(String onclick) {
        this.onclick = onclick;
    }
}
